var datagrid = null,fieldDataGrid = null, querDataGrid = null, selectedRow = null;

$(function() {
	datagrid = $('#datagrid').treegrid({
		url : host + 'moduleAction/getModuleList.html',
		fit : true,
		fitColumns : true,
		border : false,
		pagination : false,
		autoRowHeight: false,
		striped: true,
		nowrap : true,
		idField: "code",
		treeField: "name",
		rownumbers: true,
		frozenColumns: [],
		columns : [[
			{field : 'name', title : '标题', width : 120},
			{field : 'code', title : '编号', width : 80},
			{field : 'type', title : '模块类型', width : 50,
				formatter: function(value, row, index) {
					if (value == "module") {
						return "模型";
					} else if (value == "list") {
						return "表格";
					} else if (value == "edit") {
						return "编辑页";
					} else if (value == "tree") {
						return "树形结构";
					}
					return value;
				}
			},
			{field : 'id', title : '操作', width : 100,
				formatter : function(value, row, index) {
					var html = [];
					html.push("<a href='javascript: void(0)' onclick='editModule(\"" + value + "\")'>修改</a>&nbsp;");
					html.push("<a href='javascript: void(0)' onclick='deleteModule(\"" + value + "\")'>删除</a>&nbsp;");
					if (row.type != "") {
						html.push("<a href='javascript: void(0)' onclick='showModule(\"" + row.code + "\", \"" + row.name + "\")'>预览</a>");
					}
					return html.join("");
				}
			}
		]],
		toolbar: [{
			text : '新增',
			iconCls : 'icon-add',
			handler : function() {
				editModule();
			}
		}],
		onClickRow: function(data) {
			if (selectedRow != data) {
				fieldDataGrid.datagrid('load',{"moduleId": data.id});
				querDataGrid.datagrid('load',{"moduleId": data.id});
				selectedRow = data;
			}
			$("#fieldAddBtn").linkbutton({"disabled": false});
			$("#queryAddBtn").linkbutton({"disabled": false});
		}
	});
	initFieldGrid();
	initQueryGrid();
});
var dialog = null;
function editModule(id) {
	var param = "";
	if (id) {
		param = "?id=" + id;
	}
	dialog = baseJs.window(host + "system/setting/module/editModule.html" + param, {
		title: "编辑模块信息", width: 800, height: 400
	});
}
function deleteModule(id) {
	baseJs.confirm("确定提示", "确定删除此窗口？", function(r) {
		if (r) {
			$.post(host + "moduleAction/deleteModule.html", {"id": id}, function(data) {
				if (data.result) {
					datagrid.datagrid('reload');
				}
				alert(data.msg);
			}, "json");
		}
	});
}
function showModule(code, name) {
	baseJs.window(host + "show/index.jsp?code=" + code, {
		title: name, width: 1000, height: "80%"
	});
}